﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MFD.SharePoint.SPBackup
{
    //Builds a message
    internal class EmailBuilder
    {
        Logger logger;
        StringBuilder body;

        public EmailBuilder(Logger messages)
        {
            logger = messages;
            body = new StringBuilder();
            
        }

        private void BuildHeader()
        {
            Message msg = logger.GetLastError();
            if (msg == null)
                return;

            body.Append("Last Error:");
            body.Append("<Table>");
                BuildMessageRow(msg);
            body.Append("</Table>");
        }


        

        private void BuildContent()
        {
            body.Append("<Table>");
            logger.ForEachMessage(BuildMessageRow);
            body.Append("</Table>");
        }

        private void BuildMessageRow(Message msg)
        {
            body.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>",msg.TimeStamp, msg.MsgType, msg.Text );
        }

        private void BuildFoter()
        {

            body.AppendFormat("<P>Total Errors:{0}", logger.GetErrorcount());
        }
        

        public string Body
        {
            get 
            {
                BuildHeader();
                BuildContent();
                BuildFoter();
                return body.ToString();
            }
        }
    }
}
